package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.mapper.TransFlowMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.TransFlowService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2023-05-26
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {
    public void extracted(BigDecimal amount, Long userId, UserInfo userInfo, String chargeNo,String transTypeName) {
        TransFlow transFlow = new TransFlow();
        transFlow.setUserId(userId);
        transFlow.setUserName(userInfo.getName());
        // 交易流水号，可以使用本次交易的唯一编号设置
        transFlow.setTransNo(chargeNo);
        transFlow.setTransType(1);
        transFlow.setTransTypeName(transTypeName);
        transFlow.setTransAmount(amount);
        //备注信息：可以在回调中更新
//        transFlow.setMemo();
        transFlow.setStatus(0);
        this.save(transFlow);
    }

    @Override
    public TransFlow getTransFlowByAgentBillNo(String agentBillNo) {
        return this.getOne(Wrappers.lambdaQuery(TransFlow.class)
                .eq(TransFlow::getTransNo,agentBillNo));
    }

    @Override
    public List<TransFlow> listByUserId(String token) {
        //失败的交易记录是否查询：全查
        //排序： 按照时间降序排列
        Long userId = JwtUtils.getUserId(token);

        return this.list(Wrappers.lambdaQuery(TransFlow.class)
                .eq(TransFlow::getUserId,userId)
                .orderByDesc(TransFlow::getCreateTime));
    }
}
